//
//  WSDSGSecondViewController.swift
//  AnimationTableView
//
//  Created by 刘爽 on 16/11/4.
//  Copyright © 2016年 WSDSG. All rights reserved.
//

import UIKit

class WSDSGSecondViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

    var mainTableView: UITableView?
    var tableData = ["Read 3 article on Medium", "Cleanup bedroom", "Go for a run", "Hit the gym",
        "Build another swift project", "Movement training", "Fix the layout problem of a client project",
        "Write the experience of #30daysSwift", "Inbox Zero", "Booking the ticket to Chengdu",
        "Test the Adobe Project Comet", "Hop on a call to mom",
        "What the Fuck",
        "Fuck You Baby",
        "Hi, Sweety Pussy",
        "You MatherFucker",
        "WHC_AutoLayout",
        "WHC_DataFactory",
        "WHC_BannerViewKit",
        "WHC_HttpKit",
        "WHC_DataModel",
        "WHC_ModelSqlKit",
        "WHC_Download",
    ]
    
    
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.hidden = true
        animationTable()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.mainTableView = UITableView(frame: self.view.bounds, style: UITableViewStyle.Plain)
        mainTableView?.backgroundColor = UIColor.whiteColor()
        mainTableView?.tableFooterView = UIView(frame: CGRectZero)
        mainTableView?.registerClass(WSDSGFirstTableViewCell.self , forCellReuseIdentifier: "cell")
        mainTableView?.delegate = self
        mainTableView?.dataSource = self
        
        self.view.addSubview(self.mainTableView!)
        // Do any additional setup after loading the view.
    }
    
    
    func animationTable() ->Void {
        mainTableView?.reloadData()
        let cells = mainTableView?.visibleCells
        let tableHeight: CGFloat = (mainTableView?.bounds.size.height)!
        for (index,cell) in cells!.enumerate() {
            cell.transform = CGAffineTransformMakeTranslation(0, tableHeight)
            
            UIView.animateWithDuration(1.0, delay: 0.05 * Double(index), usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { () -> Void in
                cell.transform = CGAffineTransformMakeTranslation(0, 0)
                }, completion: nil)
        }
        
    }
    
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return tableData.count
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        
        cell.backgroundColor = UIColor.clearColor()
        cell.textLabel?.textColor = UIColor.whiteColor()
        cell.textLabel?.text = tableData[indexPath.row]
        cell.textLabel?.font = UIFont(name: "Avenir Next", size: 18)
        cell.selectionStyle = .Gray
        return cell
    }
    
    func colorForIndex(index: Int) ->UIColor {
        let itemCount = tableData.count - 1
        let color = CGFloat(index) / CGFloat(itemCount)
        return UIColor(red: color, green: 0, blue: 1.0, alpha: 1.0)
    }

    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        cell.backgroundColor = colorForIndex(indexPath.row)
    }
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        let cell = tableView.cellForRowAtIndexPath(indexPath)
        cell?.setSelected(false , animated: true)
        navigationController?.popViewControllerAnimated(true)
    }
}
